#include "tangramwidget.h"
#include "mygraphicsscene.h"
#include <QtGui>

TangramWidget::TangramWidget(QWidget *parent) :
    QWidget(parent)
{
    QSizePolicy sizePolicy = this->sizePolicy();
    sizePolicy.setHorizontalPolicy(QSizePolicy::Fixed);
    sizePolicy.setVerticalPolicy(QSizePolicy::Fixed);
    setSizePolicy(sizePolicy);

    s = 200;

    scene = new MyGraphicsScene();
    scene->setSceneRect(this->frameGeometry());
    populateScene();

    view = new QGraphicsView();
    view->setScene(scene);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(view);
    setLayout(layout);
}

void TangramWidget::populateScene()
{
    QVector<QPointF> points;
    QColor color;
    QTime time = QTime::currentTime();
    qsrand((uint)time.msec());

    points << QPointF(0, 0) << QPointF(s/4, -s/4) << QPointF(s/2, 0);
    itemA = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemA->setBrush(color);
    itemA->setPen(color);
    itemA->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemA->setAcceptsHoverEvents(true);

    points.clear();
    points << QPointF(s/2, 0) << QPointF(s, 0) << QPointF(s, -s/2);
    itemB = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemB->setBrush(color);
    itemB->setPen(color);
    itemB->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemB->setAcceptsHoverEvents(true);

    points.clear();
    points << QPointF(s/4, -s/4) << QPointF(s/2, 0) << QPointF(3*s/4, -s/4) << QPointF(s/2, -s/2);
    itemC = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemC->setBrush(color);
    itemC->setPen(color);
    itemC->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemC->setAcceptsHoverEvents(true);

    points.clear();
    points << QPointF(0, 0) << QPointF(s/2, -s/2) << QPointF(0, -s);
    itemD = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemD->setBrush(color);
    itemD->setPen(color);
    itemD->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemD->setAcceptsHoverEvents(true);

    points.clear();
    points << QPointF(s/2, -s/2) << QPointF(3*s/4, -s/4) << QPointF(3*s/4, -3*s/4);
    itemE = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemE->setBrush(color);
    itemE->setPen(color);
    itemE->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemE->setAcceptsHoverEvents(true);

    points.clear();
    points << QPointF(3*s/4, -s/4) << QPointF(s, -s/2) << QPointF(s, -s) << QPointF(3*s/4, -3*s/4);
    itemF = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemF->setBrush(color);
    itemF->setPen(color);
    itemF->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemF->setAcceptsHoverEvents(true);

    points.clear();
    points << QPointF(0, -s) << QPointF(s/2, -s/2) << QPointF(s, -s);
    itemG = new QGraphicsPolygonItem(points, 0, scene);
    color = QColor(qrand()%255, qrand()%255, qrand()%255);
    itemG->setBrush(color);
    itemG->setPen(color);
    itemG->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    itemG->setAcceptsHoverEvents(true);
}

void TangramWidget::setSize(int s)
{
    this->s = s;
}

int TangramWidget::size()
{
    return s;
}

void TangramWidget::contextMenuEvent(QContextMenuEvent * event)
{
    selectedItem = dynamic_cast<QGraphicsPolygonItem*>(scene->itemAt(view->mapToScene(event->pos())));
    if (selectedItem != 0) {
        QMenu menu;
        QAction *rotateAction = menu.addAction("Rotate");
        QAction *changeColor = menu.addAction("Color");
        QAction *selectedAction = menu.exec(event->globalPos());
        if (selectedAction == rotateAction) {
            selectedItem->setTransformOriginPoint(selectedItem->boundingRect().center());
            selectedItem->setRotation(selectedItem->rotation()+45);
        } else if (selectedAction == changeColor) {
            QColor color;
            color = QColorDialog::getColor(color, this );
            if(color.isValid()) {
                selectedItem->setPen(color);
                selectedItem->setBrush(color);
            }
        }
    }
}

